//
//  LHDeviceManger.m
//  LazyHome
//
//  Created by yh on 2018/5/8.
//  Copyright © 2018年 yh. All rights reserved.
//

#import "LHDeviceManger.h"
#import "TCPManager.h"
#import "LHCmd.h"
#import "TCPClient.h"
#import "LHMessage.h"
#import <MJExtension/MJExtension.h>
#import "DeviceTCPManager.h"
#import "NSMutableData+BufferHandler.h"
#import "LHDeviceInfo.h"
NSString *const NOTI_DEVINFOCHANGE = @"NOTI_DEVINFOCHANGE";
@implementation LHDeviceManger

+ (instancetype)shareManager{
    static dispatch_once_t onceToken;
    static LHDeviceManger *shareInstance = nil;
    dispatch_once(&onceToken, ^{
        shareInstance = [[LHDeviceManger alloc] init];
    });
    return shareInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageCameWithNoti:) name:NOTI_TCP_MESSAGE_CAME object:nil];
    }
    return self;
}

- (void)messageCameWithNoti:(NSNotification *)noti{
    LHMessage *msg = noti.object;
    NSString *contentString =[[NSString alloc]initWithData:msg.data encoding:NSUTF8StringEncoding];
    ushort cmd = [msg.cmdData toUShort];
    NSDictionary *content = [contentString mj_JSONObject];
    
    if (cmd == CMD_S2P_DEVICESTATUS) {
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_DEVINFOCHANGE object:content];
    }else
    if (cmd == CMD_S2P_DEVICES) {
        self.myDeviceIds = content[@"deviceIds"];
    }
}


@end
